﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using Commerce.Common;
using System.Reflection;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class editCoupon : umbraco.BasePages.UmbracoEnsuredPage
    {
        private umbraco.controls.datePicker ExpirationDate;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                CouponType.Items.Clear();
                CouponTypeCollection coupontypes = new CouponTypeCollection().OrderByAsc("Description").Load();               
                CouponType.Items.Add(new ListItem("choose", "0"));
                foreach (CouponType thisCouponType in coupontypes)
                {
                    CouponType.Items.Add(new ListItem(thisCouponType.Description, thisCouponType.CouponTypeId.ToString()));
                }

                
                string couponCode = Request.QueryString.Get("c");
                Coupon thisCoupon = new Coupon("CouponCode", couponCode);

                CouponCode.Text = thisCoupon.CouponCode;
                CouponType.SelectedValue = thisCoupon.CouponTypeId.ToString();
                IsSingleUse.Checked = thisCoupon.IsSingleUse;
                if (thisCoupon.ExpirationDateHasValue)
                    ExpirationDate.DateTime = thisCoupon.ExpirationDate;
                else
                    ExpirationDate.EmptyDateAsDefault = true;
            }

            this.Panel1.Text = "Edit Coupon";
            this.pp_couponcode.Text = "Coupon Code";
            this.pp_coupontype.Text = "Coupon Type";
            this.pp_issingleuse.Text = "Is Single Use";
            this.pp_expirationdate.Text = "Expiration Date";
        }

        private void InitializeComponent()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            //Add expiration date control
            ExpirationDate = new datePicker();
            ExpirationDate.ID = "expirationdate";
            ExpirationDate.ShowTime = true;

            Panel2.Controls.Add(ExpirationDate);


            //Add controls specific to type of coupon

            Commerce.Promotions.Coupon coup = Commerce.Promotions.Coupon.GetCoupon(Request.QueryString.Get("c"));

            Type coupType = coup.CouponType.CouponClassType;
            PropertyInfo[] pi = coupType.GetProperties();

            foreach (PropertyInfo info in pi)
            {
                if (info.Name != "CouponCode" && info.Name != "CouponType" &&
                    info.Name != "IsSingleUse" && info.Name != "UserId" &&
                    info.Name != "NumberOfUses" && info.Name != "ExpirationDate")
                {

                    umbraco.uicontrols.PropertyPanel proppan = new umbraco.uicontrols.PropertyPanel();
                    proppan.Text = info.Name;

                    TextBox txtValue = new TextBox();
                    txtValue.CssClass = "guiInputText";
                    txtValue.Width = 300;
                    txtValue.Text = info.GetValue(coup, null).ToString();
                    txtValue.ID = info.Name;

                    proppan.Controls.Add(txtValue);


                    Panel3.Controls.Add(proppan);

                    //string testq = info.Name;
                    //object testa = ;

                }
            }

            

            



            //Add Save button
            ImageButton button = this.Panel1.Menu.NewImageButton();
            button.Click += new ImageClickEventHandler(this.save_click);
            button.AlternateText = "Save";
            button.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";
            base.Load += new EventHandler(this.Page_Load);
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void save_click(object sender, ImageClickEventArgs e)
        {
            if (this.SaveCoupon(base.Request.QueryString["c"]))
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemSaved"), "");
            }
            else
            {
                base.speechBubble(0, ui.Text("speechBubbles", "commerceItemErrorHeader"), ui.Text("speechBubbles", "commerceItemErrorText"));
            }
        }

        private bool SaveCoupon(string couponCode)
        {
            try
            {
                
                Coupon thisCoupon = new Coupon("CouponCode",couponCode);
                thisCoupon.CouponCode = CouponCode.Text;
                if (CouponType.SelectedValue != "0")
                    thisCoupon.CouponTypeId = int.Parse(CouponType.SelectedValue);
                thisCoupon.IsSingleUse = IsSingleUse.Checked;
                if (ExpirationDate.DateTime.Year != 1)
                    thisCoupon.ExpirationDate = ExpirationDate.DateTime;
                

                Commerce.Promotions.Coupon temp = Commerce.Promotions.Coupon.GetCoupon(CouponCode.Text);

                object testing = Activator.CreateInstance(temp.CouponType.CouponClassType);

                Type test = temp.CouponType.CouponClassType;
                PropertyInfo[] pi = test.GetProperties();

                foreach (PropertyInfo info in pi)
                {
                    if (info.Name != "CouponCode" && info.Name != "CouponType" &&
                        info.Name != "IsSingleUse" && info.Name != "UserId" &&
                        info.Name != "NumberOfUses" && info.Name != "ExpirationDate")
                    {

                        if (Panel3.FindControl(info.Name) != null)
                        {
                            TextBox value = (TextBox)Panel3.FindControl(info.Name);                

                            info.SetValue(temp, Convert.ChangeType(value.Text, info.PropertyType), null);
                        }
                     
                       
                    }
                }             
               
                thisCoupon.XmlData = temp.GetXmlData();                
                thisCoupon.Save();
                

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
